// pages/userinfo/userInfo.js
const app = getApp();
Page({
  data: {
    bgTextStyle: 'dark',
    scrollTop: '200rpx',
    bgColor: '#ff0000',
    bgColorTop: '#00ff00',
    bgColorBottom: '#0000ff',
    nbTitle: '个人信息',
    nbLoading: false,
    nbFrontColor: '#000000',
    nbBackgroundColor: '#ffffff',
    pageStyle: "color:#333333;background-color: #f1f1f1;",
    navs:[
      {title: "首页",class:"", path:"/pages/index/index", icon: "", font: "fa fa-home"},
      {title: "信息",class:"", path:"", icon: "", font: "fa fa-comments-o"},
      {title: "维修",class:"", path:"", icon: "", font: "fa fa-wrench"},
      {title: "我的",class:"co", path:"/pages/mine/mine", icon: "", font: "fa fa-user-circle"},
    ],
    userInfo: {
      nickName: "",
      addr: "",
      phone: "",
      avatar: ""
    },
    hasChange: false,
    ready: false
  },
  onLoad: function (options) {
  },
  onReady: function () {
  },
  onShow: function () {
    let v = this;
    if (app.globalData.token){
      v.getUserInfo();
    } else {
      app.doLogin(v,v.getUserInfo);
    }
  },
  bindInput(ev) {
    console.log('bindInput', ev);
    let v = this;
    let cd = v.data.userInfo;
    let ds = ev.currentTarget.dataset;
    if(ds.key){
      let current = cd[ds.key];
      let newVal  = ev.detail.value;
      console.log('current',current,newVal);
      if(newVal != current) {
        cd[ds.key] = newVal;
        // v.setData({userInfo: cd});
        v.setData({userInfo: cd, hasChange: true});
        // v.saveInfo();
      }
    }
  },
  onChooseAvatar (ev) {
    console.log("onChooseAvatar", ev);
    const v = this;
    const img = ev.detail.avatarUrl;
    const url = app.globalData.host + "/api/common/upimg";
    const info = v.data.userInfo;
    if (url) {
      wx.uploadFile({
        url: url, //仅为示例，非真实的接口地址
        filePath: img,
        name: 'file',
        formData: {'user': info.user_id },
        success (res){
          let data = res.data
          if(typeof data == 'string') data = JSON.parse(data);
          console.log(data,res);
          let images = v.data.images;
          if (data.code == 200) {
            console.log("uploadFile",data);
            const src = data.data.src;
            info.avatar = src;
            v.setData({userInfo: info, hasChange: true});
            // v.saveInfo();
          }
        },
        fail (err) {
          console.log("fail", err);
        }
      })
    }
  },
  pickAvatar(ev) {
    console.log("pickAvatar", ev);

  },
  saveInfo(){
    let v= this;
    let url = app.globalData.host + '/api/user/upinfo';
    let data = {token: app.globalData.token};
    let info = v.data.userInfo;
    if(info.nickName) data.nickName = info.nickName;
    if(info.avatar) data.avatarUrl = info.avatar;
    if(info.gender) data.gender = info.gender;
    if(info.phone) data.phone = info.phone;
    wx.request({
      url: url, data: data,method: "POST",
      success (res) {
        let r = res.data;
        if(r.code == 200 && r.data) {
          console.log("saveUserInfo",r);
          // if(r.msg) wx.showToast({ icon: "none", title: r.msg });
          v.getUserInfo();
        }
      },
      fail (err) {
        wx.showToast({ icon: "none", title: '保存失败，请稍后再试~~', });
        console.log('saveInfo',err);
      }
    })
  },
  getUserInfo(){
    let v = this;
    v.setData({hasChange: false});
    let url = app.globalData.host + '/api/user/getinfo';
    wx.request({
      url: url, data: {token: app.globalData.token},
      success (res) {
        let r = res.data;
        if(r.code == 200 && r.data) {
          let l = r.data.userInfo;
          if(l) {
            let info = v.data.userInfo;
            for(let k in l) info[k] = l[k];
            if(l.nickname) info.nickName = l.nickname;
            console.log("getUserInfo",info);
            v.setData({userInfo: info});
          }
        }
      }
    })
  },
  navTap(ev){
    let v = this;
    let ds = ev.currentTarget.dataset;
    let ll = v.data.navs;
    if(ds && ll[ds.idx]) {
      let lk = ll[ds.idx];
      let path = '/';
      if(lk){
        if(lk.path) {
          path = lk.path ;
          wx.reLaunch({ url: path});
        }
      }
      console.log('navTap',path, lk);
    }
  },

  onHide: function () {
  },
  onUnload: function () {
  },
  onPullDownRefresh: function () {
  },
  onReachBottom: function () {
  },

  onShareAppMessage: function () {
  }
})